﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Web.Mvc;
using AutoMapper;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.Application.Platform;
using Sharp.DataTransferObject;
using Sharp.Domain.Repositories;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Infrastructure.IoC;
using Sharp.ServiceContracts;
using Sharp.Web.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;
#pragma warning disable 618

#endregion

namespace Sharp.Web.Controllers
{
    public class MasterDataController : Controller
    {
        // GET: Manage/MasterData
        public ActionResult Index(string containerId)
        {
            var model = new MasterDataIndexModel();
            model.LeftTree = new Node()
            {
                Text = ".",
                NodeID = "Root",
                Expanded = true,
                AutoDataBind = false,
                Checked = null
            };

            var builder = new TreeBuilder2<MasterDataDTO>
            {
                ChangeData = (p, treeNode) =>
                {
                    treeNode.Text = p.Name;
                    treeNode.NodeID = p.Id.ToString();
                    treeNode.Qtip = p.Code;
                    treeNode.CustomAttributes.Add(new ConfigItem() { Name = "FullName", Value = p.FullName });
                    treeNode.Expanded = true;
                }
            };
            IList<MasterDataDTO> list;
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                  list = proxy.Channel.GetMasterDataSort();
            }
            builder.Build(list, model.LeftTree, null);

            return new PartialViewResult
            {
                Model = model,
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }

        public ActionResult Create()
        {
            return new PartialViewResult { ViewName = "Edit", Model = new MasterDataModel() };
        }
        public ActionResult Edit(Guid id)
        {
            MasterDataModel vm;
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                vm = proxy.Channel.GetMasterDataInfo(id).To<MasterDataModel>();
                vm.DropDown = proxy.Channel.GetMasterData();
            }

            return new PartialViewResult { ViewName = "Edit", Model = vm };
        }
        public ActionResult Details(Guid id)
        {
            MasterDataModel vm;
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                vm = proxy.Channel.GetMasterDataInfo(id).To<MasterDataModel>();
                vm.DropDown = proxy.Channel.GetMasterData();
            }

            return new PartialViewResult { ViewName = "Edit", Model = vm };
        }
        [HttpPost]
        public ActionResult Save(MasterDataModel model)
        {
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                proxy.Channel.SaveMasterData(model.To<MasterDataDTO>());

                //return this.JsonNet(res);
            }

            return this.FormPanel();
        }

        #region 分页数据
        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(StoreRequestParameters parameters)
        {
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                var pageInfo = proxy.Channel.MasterDataGetPaged(parameters.ToPagingFeature());
                 
                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }

            
        }
        #endregion

        [HttpPost]
        public JsonResult Delete(Guid[] ids)
        {
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                var res = proxy.Channel.DeleteMasterData(ids);

                return Json(res);
            }
        }

    }
}